package com.Rolin.wss.controller;

import com.Rolin.wss.dao.SeatMapper;
import com.Rolin.wss.model.FlightPOJO;
import com.Rolin.wss.model.JspToController.SeatBO;
import com.Rolin.wss.model.SeatOrderVO;
import com.Rolin.wss.model.UserVO;
import com.Rolin.wss.service.FlightService;
import com.Rolin.wss.service.SeatService;
import com.Rolin.wss.util.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/seat")
public class SeatController {
    @Autowired
    SeatService seatService;
    @Autowired
    FlightService flightService;

    @RequestMapping(value = "/buy-seat.do",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String buySeat(@RequestBody SeatBO sbo, HttpSession session){
        System.out.println(sbo.toString());
        UserVO user = (UserVO) session.getAttribute("user");
        //获取票号
        int Ticket = (int) session.getAttribute("Ticket");

        FlightPOJO fpojo = flightService.getTicketInfo(Ticket);

        SeatOrderVO seatOrderVO = new SeatOrderVO();
        seatOrderVO.setSo_user_id(user.getUser_id());
        seatOrderVO.setSo_flight_id(fpojo.getFo_flight().getFlight_id());
        seatOrderVO.setSo_seat_no_row(sbo.getSo_seat_no_row());
        seatOrderVO.setSo_seat_no_col(sbo.getSo_seat_no_col());

        boolean res = seatService.bySeat(seatOrderVO);
        if(res){
            return utils.buildJSONForReturn(0,"success");
        }else{
            return utils.buildJSONForReturn(1,"fail");
        }
    }
}
